package yyh.guanganmen.server;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import yyh.guanganmen.common.FileUtil;
import yyh.guanganmen.db.ExpertCorePrescriptionDB;
import yyh.jaccard.bean.ExpertCorePrescriptionBean;



/**
 * @author YuanYuhu
 * 1：这个程序用来把专家核心方放入到数据库中
 * 2：由于UTF-8的.txt文件中的首行有默认的一个符号，所以需要对首行的数据进行处理
 *
 */
public class CreateExpertCorePrescription {
	public static final String SinglePath = "E:\\Zcore_fangji_path";
	
	/**
	 * 1：先读文件，把所有的信息读到list中；
	 * 2：把list中的数据插入到数据库中
	 * 3：得到药物名中含有"？"的名称信息
	 * 4：修改这部分的信息 ：添加一个标记，然后修改该标记下的数据，然后再删除标记即可
	 */
	public void run(String SinglePath) throws IOException, SQLException{
		ExpertCorePrescriptionDB db = new ExpertCorePrescriptionDB();
		List<ExpertCorePrescriptionBean> list =  function(SinglePath);
		insertIntoDatabase(list);
		db.dataClean();
	}
	
	
	/**
	 * 2：把ExpertCorePrescriptionBean中的内容放入到数据库中
	 * 把文档数据插入到数据库中
	 * @param ecpBeanList
	 */
	public void insertIntoDatabase(List<ExpertCorePrescriptionBean> ecpBeanList){
		try {
			new ExpertCorePrescriptionDB().insertPrescriptionInfo(ecpBeanList);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	/**
	 * 1：先用这个程序把文件中个内容拿出来
	 * 把该路径下的所有的文件打包成List；
	 * 这里使用了文件的树的结构
	 * 并对文件树进行了遍历
	 * @param SinglePath
	 * @return
	 * @throws IOException
	 */
	public List<ExpertCorePrescriptionBean> function(String SinglePath) throws IOException{
		List<ExpertCorePrescriptionBean> ecpBeanList = new ArrayList<ExpertCorePrescriptionBean>();
		List<File> fileList = FileUtil.getFile(SinglePath);
		while(fileList.size()!=0){
			File file = fileList.get(0);
			if(file.isDirectory()){
				fileList.addAll(FileUtil.getFile(file.getAbsolutePath()));
			}else{
				String fileName = file.getName();
				fileName = fileName.substring(0, fileName.indexOf('.'));
				String fileParentName = file.getParentFile().getName();
				BufferedReader reader  = FileUtil.getReader(file.getAbsolutePath());
				String txtLine = new String();
				while((txtLine = reader.readLine())!=null){
					ExpertCorePrescriptionBean ecpBean = new ExpertCorePrescriptionBean();
					ecpBean.setFangji_name(fileParentName);
					ecpBean.setInhosptial_id(fileName);
					ecpBean.setCase_srccode(getCase_srccode(fileName));
					ecpBean.setTransformed_gvalue(txtLine.trim());
					ecpBeanList.add(ecpBean);
				}
				if(reader!=null){
					reader.close();
				}
			}
			fileList.remove(0);
		}
		return ecpBeanList;
	}
	
	public String getCase_srccode(String fileName){
		if(fileName.startsWith("601")){
			return "60000601";
		}else if(fileName.startsWith("602")){
			return "60000602";
		}else if(fileName.startsWith("603")){
			return "60000603";
		}else if(fileName.startsWith("604")){
			return "60000604";
		}else if(fileName.startsWith("605")){
			return "60000605";
		}else if(fileName.startsWith("606")){
			return "60000606";
		}else if(fileName.startsWith("607")){
			return "60000607";
		}else if(fileName.startsWith("608")){
			return "60000608";
		}else if(fileName.startsWith("609")){
			return "60000609";
		}else{
			return "";
		}
	}
	
	
	
	public static void main(String[] args) throws IOException, SQLException {
		// TODO Auto-generated method stub
		CreateExpertCorePrescription c = new CreateExpertCorePrescription();
		c.run(SinglePath);
	}

}









